﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyModels;

namespace MyApi.Controllers
{
    /// <summary>
    /// 控制器基类
    /// </summary>
    public class BaseApiController : Controller
    {

        /// <summary>
        ///  返回成功
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        [NonAction]
        public MessageModel<T> Success<T>(T data,string msg="成功")
        {
            return new MessageModel<T>
            {
                success = true,
                data = data,
                message = msg
            };
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [NonAction]
        public MessageModel Success(string msg = "成功")
        {
            return new MessageModel()
            {
                Success = true,
                Message = msg,
                Data = null
            };
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="msg"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [NonAction]
        public MessageModel<T> Fail<T>(string msg="失败",int status = 500)
        {
            return new MessageModel<T>
            {
                success = false,
                status = status,
                message = msg,
                data = default(T)!
            };
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [NonAction]
        public MessageModel<string> Fail(string msg="失败",int status = 500)
        {
            return new MessageModel<string>
            {
                success = false,
                status = status,
                message = msg,
                data = null
            };
        }


    }
}
